package com.example.droideep.common.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Checkable;
import android.widget.TextView;

import com.example.droideep.R;

import droideep.library.widget.TagStrip;

/**
 * Created by bri on 15-4-24.
 */
public class ArrowTagStrip extends TagStrip {

    SparseBooleanArray mCheckStates = new SparseBooleanArray();

    private Drawable upArrow;
    private Drawable downArrow;

    public ArrowTagStrip(Context context) {
        this(context, null);
    }

    public ArrowTagStrip(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomTabView(R.layout.layout_arrow_tag, R.id.arrow_tag);
        setCustomTabColorizer(new CustomTabColorizer());

        upArrow = getContext().getResources().getDrawable(R.drawable.arrow_up);
        downArrow = getContext().getResources().getDrawable(R.drawable.arrow_down);
    }

    /**
     * @param position The item whose checked state to return
     * @return The item's checked state or <code>false</code> if choice mode
     * is invalid
     */
    public boolean isItemChecked(int position) {
        if (mCheckStates != null) {
            return mCheckStates.get(position);
        }

        return false;
    }

    @Override
    public void setTags(String[] tags) {
        super.setTags(tags);
        if (tags != null && tags.length > 0) {
            for (int i = 0; i < tags.length; i++) {
                mCheckStates.put(i, false);
            }
        }
    }

    private class CustomTabColorizer implements TabColorizer {

        @Override
        public void tabColorizer(View view) {
            Integer pos = (Integer) view.getTag();
            /**
             * 如果当前点击的Tag是true(表示被选中),则直接将其设为false,并改变箭头即可,
             * 如果当前点击的Tag是false(表示未被选中),则首先找当前被选中的Tag的位置，并将其状态设为false,并改变箭头
             */
            if (mCheckStates.get(pos)) {
                setItemChecked(pos, false);
            } else {
                int prevPos = mCheckStates.indexOfValue(true);
                if (prevPos != -1) {
                    setItemChecked(prevPos, false);
                }
                setItemChecked(pos, true);
            }
        }
    }

    public void setAllItemNoChecked() {
        for (int i = 0; i < mTags.length; i++) {
            setItemChecked(i, false);
        }
    }

    /**
     * @param position
     * @param value
     */
    public void setItemChecked(int position, boolean value) {
        TextView arrowTag = ((TextView) getChildAt(position).findViewById(R.id.arrow_tag));

        if (value) {
            arrowTag.setCompoundDrawablesWithIntrinsicBounds(null, null, upArrow, null);
            int prev = mCheckStates.indexOfValue(true);
            if (prev != -1) {
                mCheckStates.put(prev, false);
            }
            mCheckStates.put(position, true);
        } else {
            arrowTag.setCompoundDrawablesWithIntrinsicBounds(null, null, downArrow, null);
            mCheckStates.put(position, value);
        }
    }
}
